using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using dotnetCHARTING.WinForms;

namespace Budgetor.DataVizualization
{
    public partial class PieChartPanel : UserControl, IDataVizualizationPanel
    {
        public PieChartPanel()
        {
            InitializeComponent();
        }

        private void UpdateChart(Data[] dataCollection)
        {
            pieChart.DefaultSeries.DefaultElement.ShowValue = true;
            SeriesCollection seriesCollection = new SeriesCollection();

            Dictionary<string, float> pieData = GetPieData(dataCollection);

            foreach (string description in pieData.Keys)
            {
                seriesCollection.Add(
                    new Series(description,
                    new Element(description, pieData[description])));

            }

            pieChart.SeriesCollection.Clear();
            pieChart.SeriesCollection.Add(seriesCollection);
            pieChart.Invalidate();
        }

        private Dictionary<string, float> GetPieData(Data[] dataCollection)
        {
            Dictionary<string, float> pieData = new Dictionary<string,float>();

            foreach (Data dataItem in dataCollection)
            {
                if (dataItem.Category == "Advance")
                    continue;

                if (pieData.ContainsKey(dataItem.Description))
                    pieData[dataItem.Description] += dataItem.AmountSpent;
                else
                    pieData.Add(dataItem.Description, dataItem.AmountSpent);
            }

            return pieData;
        }

        #region IDataVizualizationPanel Members

        public void UpdateData(object sender, DataEventArgs e)
        {
            UpdateChart(e.DataCollection);
        }

        #endregion
    }
}
